//
//  LineCell.m
//  LuCaiZhu
//
//  Created by 邓龙 on 2019/4/25.
//

#import "LineCell.h"
#import "XLineChartView.h"
#import "DBHelper.h"
#import "UIColor+NN.h"

@interface LineCell()

@property (weak, nonatomic) IBOutlet UIView *picView;
@property (weak, nonatomic) IBOutlet UILabel *line2;
@property (weak, nonatomic) IBOutlet UILabel *lineName;
@property (nonatomic, copy) NSString *ishave;

@property (nonatomic, strong) XLineChartView *chartView;

@end

@implementation LineCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    _ishave = @"0";
    _chartView = [[XLineChartView alloc] initWithFrame:self.picView.bounds];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)sortMyData:(NSArray *)LineDataArray {
    self.line2.hidden=YES;
    self.lineName.hidden=YES;
    _chartView.xAssistLineColor=[UIColor whiteColor];//纵向辅助线颜色
    _chartView.strokeLineWidth=1;//线条宽度
    _chartView.coordColor=[UIColor lightGrayColor];
    _chartView.coordWidth=0.5;//坐标轴颜色
    _chartView.yLabNumber=5;//Y分块个数
    _chartView.animationEnable=NO;
    _chartView.chartType = XChartLineTypeCurve;//设置绘制类型为曲线
    _chartView.showDot = NO;   //设置显示圆点
    _chartView.gradientEnable = NO;  //设置启用渐变填充
    NSMutableArray *xTitles = [NSMutableArray array];
    NSMutableArray *yValues = [NSMutableArray array];
    
    for (UIView *vc in self.picView.subviews) {
        [vc removeFromSuperview];
    }
    if (LineDataArray.count==0) {
        return;
    }
    
    float max=[LineDataArray[0][@"Maximum"] floatValue];
    float min=[LineDataArray[0][@"Minimum"] floatValue];
    float sum=max-min;
    if (sum==0) {
        if ([LineDataArray[0][@"Reference"] isEqualToString:@"无"]) {
            self.line2.hidden=YES;
            self.lineName.hidden=YES;
        }else{
            self.line2.hidden=NO;
            self.lineName.hidden=NO;
            NSString *nameSS=[NSString stringWithFormat:@"%@增长率",LineDataArray[0][@"Reference"]];
            self.lineName.text=nameSS;
        }
        return;
    }
    float sizeH = sum/5;
    //    NSLog(@"sizeH==%d",sizeH);
    NSString *hh1=[NSString stringWithFormat:@"%.2f",min];
    NSString *hh2=[NSString stringWithFormat:@"%.2f",min+sizeH];
    NSString *hh3=[NSString stringWithFormat:@"%.2f",min+sizeH*2];
    NSString *hh4=[NSString stringWithFormat:@"%.2f",min+sizeH*3];
    NSString *hh5=[NSString stringWithFormat:@"%.2f",min+sizeH*4];
    NSString *hh6=[NSString stringWithFormat:@"%.2f",max];
    NSMutableArray *leftdataA = [NSMutableArray new];
    [leftdataA addObject:hh1];
    [leftdataA addObject:hh2];
    [leftdataA addObject:hh3];
    [leftdataA addObject:hh4];
    [leftdataA addObject:hh5];
    [leftdataA addObject:hh6];
    
    
    NSMutableArray* chartData = [NSMutableArray arrayWithCapacity:LineDataArray.count];
    NSMutableArray* chartData2 = [NSMutableArray arrayWithCapacity:LineDataArray.count];
    NSMutableArray* chartData3 = [NSMutableArray arrayWithCapacity:LineDataArray.count];
    NSArray* reversedArray = [[LineDataArray reverseObjectEnumerator] allObjects];
    for (int i=0; i<reversedArray.count; i++) {
        
        [chartData addObject:reversedArray[i][@"SumRose"]];
        [chartData2 addObject:reversedArray[i][@"Rose"]];
        [xTitles addObject:@""];
        NSDate *dateTime =  [DBHelper dateFromCustomString:reversedArray[i][@"DocDate"] format:@"yyyy/MM/dd HH:mm:ss"];
        NSString *time = [DBHelper stringFromCustomFormatDate:dateTime format:@"yyyy-MM-dd"];
        [chartData3 addObject:time?time:reversedArray[i][@"DocDate"]];
    }
    [yValues addObject:chartData];
    NSArray *nameTitleArray=[NSArray array];
    NSArray *colorA = [NSArray array];
    if ([LineDataArray[0][@"Reference"] isEqualToString:@"无"]) {
        self.line2.hidden=YES;
        self.lineName.hidden=YES;
        colorA=@[[UIColor colorWithHexString:@"#65AFF7"]];
        nameTitleArray=@[@"产品净值增长率"];
    }else{
        self.line2.hidden=NO;
        self.lineName.hidden=NO;
        NSString *nameSS=[NSString stringWithFormat:@"%@增长率",LineDataArray[0][@"Reference"]];
        self.lineName.text=nameSS;
        [yValues addObject:chartData2];
        colorA=@[[UIColor colorWithHexString:@"#65AFF7"],[UIColor blackColor]];
        nameTitleArray=@[@"产品净值增长率",nameSS];
    }
    
    
    _chartView.chartColors=colorA;
    _chartView.xTitles = chartData3;    //设置X
    _chartView.yValuesArray = yValues;  //设置Y
    _chartView.yNameArr=leftdataA;
    _chartView.xDateArrays=chartData3;
    _chartView.dataNameArr=nameTitleArray;
    _chartView.markerLineColor=[UIColor colorWithHexString:@"#BB3535"];
    _chartView.scaleType=XChartViewScaleTypeNone;
    
    [self.picView addSubview:_chartView];
    if ([_ishave isEqualToString:@"0"]) {
        [_chartView strokeChart];  //绘制曲线
        _ishave = @"1";
    }
}

@end
